//
//  HomeViewController.m
//  Meng
//
//  Created by dllo on 15/9/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "HomeViewController.h"
#import "TItleScrollView.h"
#import <AFNetworking.h>
#import "HomeModel.h"
#import "CollectionView.h"
#import "CollectionCell.h"
#import "HomeWebViewController.h"
#import <MJRefresh.h>
#import <RESideMenu.h>
@interface HomeViewController () <UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate>
@property (nonatomic, retain) NSMutableArray *arrScroll;
@property (nonatomic, retain) CollectionView *collection;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSMutableArray *arr1;
@property (nonatomic, retain) NSMutableArray *arr2;
@property (nonatomic, retain) TItleScrollView *scrollView;
@end

@implementation HomeViewController
- (void)dealloc
{
    [_arrScroll release];
    [_collection release];
    [_arr release];
    [_arr1 release];
    [_arr2 release];
    [_scrollView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSNotificationCenter *center1 = [NSNotificationCenter defaultCenter];

    if ([[user objectForKey:@"day"] isEqualToString:@"2"]) {
        NSNumber *nub = [[NSNumber alloc] initWithFloat:0.5];
        [center1 postNotificationName:@"nightModel" object:nub];
    } else {
        NSNumber *nub = [[NSNumber alloc] initWithFloat:0];
        [center1 postNotificationName:@"nightModel" object:nub];
    }
    self.TitleLabel.text = @"关注";
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self dataHandlescored];
    [self collectionViewSubView];
    [self RingdataHandel1];

    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"set"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)] autorelease];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(change:) name:@"change" object:nil];
}
- (void)change:(NSNotification *)notifi
{
    HomeWebViewController *webView = [[HomeWebViewController alloc] init];
    webView.str = notifi.object;
    [self.navigationController pushViewController:webView animated:YES];
}
- (void)collectionViewSubView
{
    self.arrScroll = [NSMutableArray array];
    self.arr = [NSMutableArray array];
    self.arr1 = [NSMutableArray array];
    self.arr2 = [NSMutableArray array];
    
    self.collection = [[CollectionView alloc] initWithFrame:CGRectMake(0, 40 * HEIGHT, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:self.collection];
    self.collection.collection.delegate = self;
    self.collection.collection.dataSource = self;
    self.collection.collection.bounces = NO;
    [self.collection.collection registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"reuse"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arrScroll.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //在嵌套还是别的尽量不要把赋值之类的 写在重用池里 否则会走很多次的
    CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    [cell setModelStr:self.arr1[indexPath.item]];
    if (indexPath.item == 0) {
        cell.ret = YES;
        cell.arrS = self.arr2;
    } else {
        cell.ret = NO;
    }
    return cell;
}
- (void)dataHandlescored
{
    NSString *arrPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"CashArr.plist"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:HomeScoreView parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableDictionary *dic = [responseObject objectForKey:@"data"];
        NSMutableArray *arr = [dic objectForKey:@"channels"];
        for (NSMutableDictionary *dictionary in arr) {
            HomeModel *model = [[HomeModel alloc] init];
            [model setValuesForKeysWithDictionary:dictionary];
            [self.arrScroll addObject:model];
            [model release];
        }
        self.scrollView = [[TItleScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40) arr:self.arrScroll];
        self.scrollView.scrollView.delegate = self;
        [self.view addSubview:self.scrollView];
        [self.scrollView release];
    
        for (HomeModel *model in self.arrScroll) {
            NSString *str = [NSString stringWithFormat:@"%@%ld%@", other, model.idu, other1];
            
            [self.arr1 addObject:str];
        }
        
        for (int i = 0; i < self.arr1.count; i++) {
            UIButton *button = (UIButton *)[self.scrollView viewWithTag:i + 10];
            [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
        }
        [self.collection.collection reloadData];
        
        [arr writeToFile:arrPath atomically:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSArray *arr = [NSArray arrayWithContentsOfFile:arrPath];
        for (NSMutableDictionary *dictionary in arr) {
            HomeModel *model = [[HomeModel alloc] init];
            [model setValuesForKeysWithDictionary:dictionary];
            [self.arrScroll addObject:model];
            [model release];
        }
        self.scrollView = [[TItleScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40) arr:self.arrScroll];
        self.scrollView.scrollView.delegate = self;
        [self.view addSubview:self.scrollView];
        [self.scrollView release];
        
        for (HomeModel *model in self.arrScroll) {
            NSString *str = [NSString stringWithFormat:@"%@%ld%@", other, model.idu, other1];
            
            [self.arr1 addObject:str];
        }
        for (int i = 0; i < self.arr1.count; i++) {
            UIButton *button = (UIButton *)[self.scrollView viewWithTag:i + 10];
            [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
        }
        [self.collection.collection reloadData];
        
    }];
}
- (void)button:(UIButton *)button
{
    for (int i = 0; i < self.arr1.count; i++) {
        if (button.tag == i + 10) {
            button.titleLabel.font = [UIFont systemFontOfSize:20];
        } else {
          UIButton *button = (UIButton *)[self.scrollView viewWithTag:i + 10];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
    }
    [self.collection.collection setContentOffset:CGPointMake((button.tag - 10) * 375 * WIDTH, 0) animated:YES];
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    for (int i = 0; i < self.arr1.count; i++) {
        UIButton *button = (UIButton *)[self.scrollView viewWithTag:i + 10];
        if (button.tag == scrollView.contentOffset.x / 375 + 10) {
            button.titleLabel.font = [UIFont systemFontOfSize:20];
        } else {
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
    }
}

//轮播图
- (void)RingdataHandel1
{
    NSString *arrPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"caarr.plist"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:pic parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *arr = [[responseObject objectForKey:@"data"] objectForKey:@"banners"];
        for (NSMutableDictionary *dic in arr) {
            HomeModel *modle = [[HomeModel alloc] init];
            [modle setValuesForKeysWithDictionary:dic];
            [self.arr2 addObject:modle];
            [modle release];
        }
        [self.collection.collection reloadData];
        
        [arr writeToFile:arrPath atomically:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSArray *arr = [NSArray arrayWithContentsOfFile:arrPath];
        for (NSMutableDictionary *dic in arr) {
            HomeModel *modle = [[HomeModel alloc] init];
            [modle setValuesForKeysWithDictionary:dic];
            [self.arr2 addObject:modle];
            [modle release];
        }
        [self.collection.collection reloadData];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
